home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-07-09 | 6.9 KB | 233 lines | [TEXT/CWIE] |
- // =================================================================================
- // CPreferences.cp ©1997 BB's Team inc. All rights reserved
- // =================================================================================
- #include "CPreferences.h"
- #include "PLConstants.h"
-
- #include "CDynamicEditField.h"
- #include <UModalDialogs.h>
- #include <UResourceMgr.h>
- #include <LWindow.h>
-
-
- // ---------------------------------------------------------------------------------
- // • WritePrefs
- // ---------------------------------------------------------------------------------
- void CPreferences::WritePrefs (void)
- {
-
- Try_ {
- // open rsrc
- mFile.OpenOrCreateResourceFork (fsWrPerm, kSignature, kPrefsFileType, nil);
-
- // Change existing / Create new (StNewResource has a (Handle) accessor)
- {
- StNewResource rsrcHandle (kPrefsRsrcType, kPrefsRsrcID, sizeof (Prefs), true);
- BlockMoveData (&mPrefs, *(Handle)rsrcHandle, sizeof(Prefs));
- // ~StNewResource writes out the resource
- }
-
- // leave the rsrc fork closed
- mFile.CloseResourceFork();
- }
-
- Catch_ (inErr) {
- } EndCatch_
- }
-
-
- // ---------------------------------------------------------------------------------
- // • ReadPrefs
- // ---------------------------------------------------------------------------------
- void CPreferences::ReadPrefs (void)
- {
- Try_ {
- // open rsrc
- mFile.OpenResourceFork (fsRdPerm);
-
- // load rsrc if exists
- {
- StResource rsrcHandle (kPrefsRsrcType, kPrefsRsrcID, true, true);
- BlockMoveData (*(Handle)rsrcHandle, &mPrefs, sizeof(Prefs));
- }
-
- // leave the rsrc fork closed
- mFile.CloseResourceFork();
- }
-
- Catch_ (inErr) {
- } EndCatch_
- }
-
-
- // ---------------------------------------------------------------------------------
- // • ctor
- // ---------------------------------------------------------------------------------
- CPreferences::CPreferences()
- : mFile (kPrefsFileName)
- {
- mPrefs.sTextTraits.size = 255;
- mPrefs.sScreen = mPrefs.s7Bits
- = mPrefs.sContrast
- = mPrefs.sConfirmPrinting
- = mPrefs.sMonoSpace
- = mPrefs.sBeep32k
- = 255;
- AllowAll (mPrefs.sAlphabet);
-
- ReadPrefs();
- }
-
-
- // ---------------------------------------------------------------------------------
- // • FinishCreate
- // ---------------------------------------------------------------------------------
- void CPreferences::FinishCreate(void)
- {
- // retrieve default values from the PPob resources
- // cannot be done in the ctor : PPob classes are not registered yet !
- if ( !IsOk() ) {
- StDialogHandler theHandler (rPPob_PrefsDialog, nil);
- LWindow *theDialog = theHandler.GetDialog();
- Assert_ (theDialog != nil);
- mPrefs.sConfirmPrinting = theDialog->GetValueForPaneID (kConfirmCheck);
- mPrefs.sMonoSpace = theDialog->GetValueForPaneID (kMonoTestCheck);
- mPrefs.sBeep32k = theDialog->GetValueForPaneID (kBeep32k);
- }
- }
-
-
- // ---------------------------------------------------------------------------------
- // • dtor
- // ---------------------------------------------------------------------------------
- CPreferences::~CPreferences()
- {
- WritePrefs();
- }
-
-
- // ---------------------------------------------------------------------------------
- // • AllowAll
- // ---------------------------------------------------------------------------------
- void CPreferences::AllowAll (Str255 &inAllow)
- {
- LStr255 allow;
- allow = "\p";
-
- // word wrap in the LEditField makes a CR where the space lies
- // let's take advantage of this to distinguish between 7 and 8 bits ascii
- for (Int16 i = kFirstChar+1 ; i<128 ; i++)
- allow += (Uchar) i;
- allow += (Uchar) kFirstChar;
- for (Int16 i = 128 ; i<256 ; i++)
- allow += (Uchar) i;
-
- // but forbid from the 'Excl' rsrc (at least char 127 is a problem)
- Handle theExcl = ::GetResource ('Excl', 128);
- if (theExcl) {
-
- ::HLock (theExcl);
-
- allow.SetCompareFunc(LString::CompareBytes);
- Int16 count = *(Int16*)(*theExcl); // first one 16 bits : the size
- Uchar *array = (Uchar*)(*theExcl); // then 8 bits.
-
- for (Int16 i=2 ; i<=1+count ; ++i) { // shift 1 right for the size
- Uint8 found = allow.Find (array+i, 1, 1); // array+i : address of the i th char
- if (found)
- allow.Remove (found, 1);
- }
-
- ::HUnlock (theExcl);
- ::ReleaseResource ( theExcl );
- }
-
- LString::CopyPStr (allow, inAllow);
-
- }
-
-
- // ---------------------------------------------------------------------------------
- // • IsOk
- // ---------------------------------------------------------------------------------
- Boolean CPreferences::IsOk(void)
- {
- return mPrefs.sTextTraits.size != 255
- && mPrefs.sScreen != 255
- && mPrefs.s7Bits != 255
- && mPrefs.sContrast != 255
- && mPrefs.sConfirmPrinting != 255
- && mPrefs.sMonoSpace != 255
- && mPrefs.sBeep32k != 255;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • GetTextTraits
- // ---------------------------------------------------------------------------------
- void CPreferences::GetTextTraits (TextTraitsRecord &outTextTraits) const
- {
- ::BlockMoveData( &mPrefs.sTextTraits, &outTextTraits, sizeof(TextTraitsRecord) );
- }
-
-
- // ---------------------------------------------------------------------------------
- // • SetTextTraits
- // ---------------------------------------------------------------------------------
- void CPreferences::SetTextTraits (const TextTraitsRecord &inTextTraits)
- {
- ::BlockMoveData( &inTextTraits, &mPrefs.sTextTraits, sizeof(TextTraitsRecord) );
- }
-
-
- // ---------------------------------------------------------------------------------
- // • DoDialog
- // ---------------------------------------------------------------------------------
- void CPreferences::DoDialog (LStr255 inAllowed, const Str255 displayFont)
- {
- // no need to bother with a commander
- StDialogHandler theHandler (rPPob_PrefsDialog, nil);
-
- // Get the dialog.
- LWindow *theDialog = theHandler.GetDialog();
- Assert_ (theDialog != nil);
-
- // setup controls
- theDialog->SetValueForPaneID (kConfirmCheck , mPrefs.sConfirmPrinting);
- theDialog->SetValueForPaneID (kMonoTestCheck , mPrefs.sMonoSpace);
- theDialog->SetValueForPaneID (kBeep32k , mPrefs.sBeep32k);
-
- CDynamicEditField *theField = (CDynamicEditField*) theDialog->FindPaneByID (kCharsEditField);
- Assert_ (theField != nil);
- theField->SetDescriptor (inAllowed);
- if (*displayFont)
- theField->SetFontName (displayFont);
- theDialog->Show();
-
- while (true) {
-
- MessageT theMsg = theHandler.DoDialog();
-
- // Handle cancel message.
- if (theMsg == msg_CancelButton)
- break;
-
- // Handle OK message.
- else if (theMsg == msg_OkButton) {
- mPrefs.sConfirmPrinting = theDialog->GetValueForPaneID (kConfirmCheck);
- mPrefs.sMonoSpace = theDialog->GetValueForPaneID (kMonoTestCheck);
- mPrefs.sBeep32k = theDialog->GetValueForPaneID (kBeep32k);
- theDialog->GetDescriptorForPaneID (kCharsEditField, mPrefs.sAlphabet);
- break;
- }
-
- else if (theMsg == msg_Reset) {
- Str255 all;
- AllowAll (all);
- theDialog->SetDescriptorForPaneID(kCharsEditField, all);
- }
-
- } // while
-
- }
-